package com.xpgk.mvc.organization;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Table;
import com.xpgk.mvc.base.BaseModel;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.productPrice.Price;
import com.xpgk.mvc.user.User;

/**
 * 机构model
 * 
 */
@SuppressWarnings("unused")
@Table(tableName = Organization.table_name)

public class Organization extends BaseModel<Organization> {

	private static final long serialVersionUID = 6093344540638844878L;

	private static final Log log = Log.getLog(Organization.class);

	public static final Organization dao = new Organization().dao();

	public static final String zb = "c9aebfe4912e4c07b4c7602b935154d6";

	public static final String system = "ad6886c05b5c4117be0243d64974ac12";

	/**
	 * 表名称
	 */
	public static final String table_name = "sys_organization";

	
	
	/**
	 * 主键
	 */
	public static final String column_ids = "ids";

	/**
	 * 名称
	 */
	public static final String column_name = "name";

	/**
	 * 行政区划
	 */
	public static final String column_xzqh = "xzqh";
	/**
	 * 行政区划
	 */
	public static final String column_xzqhCodeId = "xzqhCodeId";

	/**
	 * 编号
	 */
	public static final String column_bh = "bh";

	/**
	 * 机构层级
	 */
	public static final String column_level = "level";

	/**
	 * 机构层级ids
	 */
	public static final String column_levelIds = "levelIds";

	/**
	 * 是否父节点
	 */
	public static final String column_isparent = "isparent";

	/**
	 * 父级ids
	 */
	public static final String column_parentIds = "parentIds";

	/**
	 * 序号
	 */
	public static final String column_orderid = "orderid";
	/**
	 * 序号
	 */
	public static final String column_mdbh = "mdbh";
	


	/**
	 * 图标
	 */
	public static final String column_icon = "icon";

	/**
	 * 代理人ids 对应User表ids
	 */
	public static final String column_agentIds = "agentIds";

	/**
	 * 负责人ids 对应User表ids
	 */
	public static final String column_mangerIds = "mangerIds";

	/**
	 * 价格ids 对应price表ids
	 */
	public static final String column_priceIds = "priceIds";

	/**
	 * 开业时期
	 */
	public static final String column_kyTime = "kyTime";
	
	
	/**
	 * 良性店销售标准（销售5万）
	 */
	public static final String column_lxd = "lxd";

	/**
	 * 代理日期
	 */
	public static final String column_addTime = "addTime";
	/**
	 * 字段描述：该条数据添加人员is  字段类型：character varying 长度：32
	 */
	public static final String column_createId = "createId";
	/**
	 * 字段描述：该条数据添加时间  字段类型：datetime 长度：0
	 */
	public static final String column_createDate = "createDate";
	/**
	 * 字段描述：该条数据更新人员id  字段类型：character varying 长度：100
	 */
	public static final String column_updateId = "updateId";
	/**
	 * 字段描述：该条数据更新时间  字段类型：datetime 长度：0
	 */
	public static final String column_updateDate = "updateDate";
	public static final String column_todayyzm = "todayyzm";
	
	

	/**
	 * sqlId 描述：自定义查询
	 */
	public static final String sqlId_column = "sys.organization.column";

	/**
	 * sqlId 描述：指定机构
	 */
	public static final String sqlId_findByIds = "sys.organization.findByIds";

	/**
	 * sqlId 描述：所有地区代理
	 */
	public static final String sqlId_splitPageSelect = "sys.organization.splitPageSelect";

	public static final String sqlId_splitPageFrom = "sys.organization.splitPageFrom";

	/**
	 * sqlId 描述：查询根节点
	 */
	public static final String sqlId_rootNode = "sys.organization.rootNode";

	/**
	 * sqlId 描述：根据上级节点id查询子部门信息
	 */
	public static final String sqlId_childNode = "sys.organization.childNode";

	/**
	 * sqlId 描述：根据上级节点id查询子节点数量
	 */
	public static final String sqlId_childCount = "sys.organization.childCount";

	/**
	 * sqlId 描述：机构下用户数量
	 */
	public static final String sqlId_userCount = "sys.organization.userCount";

	/**
	 * sqlId 描述：通过行政区划查找所有门店
	 */
	public static final String sqlId_findByXZQH = "sys.organization.findByXZQH";
	
	/**
	 * sqlId 描述：通过地址查找行政区划
	 */
	public static final String sqlId_findBySDXZ = "sys.organization.findBySDXZ";
	


	/**
	 * 关联查询获取代理人信息
	 * 
	 * @return
	 */
	public User getAgent() {
		return User.dao.findById(this.getStr(column_agentIds));
	}

	/**
	 * 关联查询获取负责人信息
	 * 
	 * @return
	 */
	public User getManger() {
		return User.dao.findById(this.getStr(column_mangerIds));
	}

	/**
	 * 关联查询获取机构下所有人员
	 * 
	 * @return
	 */
	public List<User> getUsers(String organIds) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", User.column_organizationids);
		String sql = getSqlByBeetl(User.sqlId_column, param);
		return User.dao.find(sql, organIds);
	}

	/**
	 * 关联查询获取上级
	 * 
	 * @return
	 */
	public Organization getParentOrgan() {
		return Organization.dao.findById(this.getStr(column_parentIds));
	}

	/**
	 * 关联查询价格表
	 * 
	 * @return
	 */
	public Price getPrice() {
		return Price.dao.findById(this.getStr(column_priceIds));
	}

	/**
	 * 关联查询xzqh
	 * 
	 * @return
	 */
	public Code getXzqh() {
		return Code.cacheGetByCode(this.getStr(column_xzqh));
	}

	/**
	 * 关联查询价格表
	 * 
	 * @return
	 */
	public List<Price> getPrices() {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", Price.column_organizationIds);
		String sql = getSqlByBeetl(Price.sqlId_column, param);
		return Price.dao.find(sql, this.getStr(column_ids));
	}

	/**
	 * 查找所有使用同一张价格表的机构
	 * 
	 * @param priceIds
	 * @return
	 */
	public List<Organization> findOrganByPriceIds(String priceIds) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("column", Organization.column_priceIds);
		String sql = getSqlByBeetl(Organization.sqlId_column, param);
		return dao.find(sql, priceIds);
	}

	/**
	 * 关联查询获取层级信息
	 * 
	 * @return
	 */
	public Record getLevelInfo() {
		String sql = "select * from sys_organ_level_info where levelIds = ?";
		return Db.findFirst(sql, this.getStr(column_levelIds));
	}
	
	/**
	 * 根据ids查出机构信息
	 * 
	 * @return
	 */
	public Record getOrganizationInfo(String ids) {
		String sql = "SELECT * FROM sys_organization WHERE ids=?";
		return Db.findFirst(sql, ids);
	}

}
